// src/store.js
import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    user: null, // 当前登录用户
    isAdmin: false,
    lang: 'zh',
    currentVideo: '/videos/taylorswift-video.mp4',
    carouselImages: [
      '/images/carousel1.jpg',
      '/images/carousel2.jpg',
      '/images/carousel3.jpg'
    ],
    albums: [
      { id: 1, title: 'Fearless', year: 2008, cover: '/images/album1.jpg' },
      { id: 2, title: '1989', year: 2014, cover: '/images/album2.jpg' },
      { id: 3, title: 'Midnights', year: 2022, cover: '/images/album3.jpg' }
    ],
    merchandise: [
      { id: 1, name: 'T恤', price: 29.99, image: '/images/merch1.jpg', stock: 100 },
      { id: 2, name: '帽子', price: 19.99, image: '/images/merch2.jpg', stock: 50 },
      { id: 3, name: '限量专辑', price: 19.99, image: '/images/album1.jpg', stock: 30 }
    ],
    users: [
      { name: 'swiftie123', password: '123456', blocked: false },
      { name: 'musiclover', password: '123456', blocked: false }
    ]
  },
  actions: {
    setUser({ commit }, userData) {
      commit('SET_USER', userData);
    },
    setLang({ commit }, lang) {
      commit('SET_LANG', lang);
    },
    updateVideo({ commit }, url) {
      commit('UPDATE_VIDEO', url);
    },
    addCarouselImage({ commit }, image) {
      commit('ADD_CAROUSEL_IMAGE', image);
    },
    removeCarouselImage({ commit }, index) {
      commit('REMOVE_CAROUSEL_IMAGE', index);
    },
    updateMerchPrice({ commit }, { id, newPrice }) {
      commit('UPDATE_MERCH_PRICE', { id, newPrice });
    },
    blockUser({ commit }, name) {
      commit('BLOCK_USER', name);
    },
    unblockUser({ commit }, name) {
      commit('UNBLOCK_USER', name);
    },
    removeUser({ commit }, name) {
      commit('REMOVE_USER', name);
    },
    resetPassword({ commit }, name) {
      commit('RESET_PASSWORD', name);
    }
  },
  mutations: {
    SET_USER(state, userData) {
      state.user = userData;
    },
    SET_LANG(state, lang) {
      state.lang = lang;
    },
    UPDATE_VIDEO(state, url) {
      state.currentVideo = url;
    },
    ADD_CAROUSEL_IMAGE(state, image) {
      state.carouselImages.push(image);
    },
    REMOVE_CAROUSEL_IMAGE(state, index) {
      state.carouselImages.splice(index, 1);
    },
    UPDATE_MERCH_PRICE(state, { id, newPrice }) {
      const item = state.merchandise.find(m => m.id === id);
      if (item) item.price = newPrice;
    },
    BLOCK_USER(state, name) {
      const user = state.users.find(u => u.name === name);
      if (user) user.blocked = true;
    },
    UNBLOCK_USER(state, name) {
      const user = state.users.find(u => u.name === name);
      if (user) user.blocked = false;
    },
    REMOVE_USER(state, name) {
      state.users = state.users.filter(u => u.name !== name);
    },
    RESET_PASSWORD(state, name) {
      const user = state.users.find(u => u.name === name);
      if (user) user.password = 'newpassword123';
    }
  },
  getters: {
    getUser: state => state.user,
    getLang: state => state.lang,
    getCurrentVideo: state => state.currentVideo,
    getCarouselImages: state => state.carouselImages,
    getAlbums: state => state.albums,
    getMerchandise: state => state.merchandise,
    getUsers: state => state.users
  }
});